package com.atde._05_behavioral_design_patterns.command.remotecontrol;


/**
 * @BelongsProject: hsp_java_design_mode
 * @BelongsPackage: com.atde._05_behavioral_design_patterns.command.remotecontrol
 * @Author: Del
 * @CreateTime: 2025-06-25  20:17
 * @Description: 命令模式：遥控器类，它持有多组命令
 * @Version: 1.0
 */
public class RemoteController {
    // 创建开命令数组
    Command[] onCommands;
    // 创建关命令数组
    Command[] offCommands;

    // 执行撤销的命令
    Command undoCommand;

    // 构造器，完成对按钮初始化
    public RemoteController() {
       onCommands = new Command[5];
       offCommands = new Command[5];

       for (int i = 0; i < 5; i++) {
           onCommands[i] = new NoCommand();
           offCommands[i] = new NoCommand();
       }
    }

    // 给我们的按钮设置需要的命令
    public void setCommand(int no, Command onCommand, Command offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

    // 按下开的按钮
    public void onButtonWasPushed(int no) {
        // 调用接收者的方法：执行方法
        onCommands[no].execute();
        // 记录这次操作，用于撤销
        undoCommand = onCommands[no];
    }

    // 按下关的按钮
    public void offButtonWasPushed(int no) {
        // 调用接收者的方法：执行方法
        offCommands[no].execute();
        // 记录这次操作，用于撤销
        undoCommand = offCommands[no];
    }

    // 撤销按钮
    public void undoButtonWasPushed() {
        // 执行撤销
        undoCommand.undo();
    }
}
